package com.becca.cake.core.common;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.context.request.ServletWebRequest;

import com.becca.cake.model.system.AdminDO;
import com.becca.cake.util.PMSConstants;
import com.becca.cake.util.StringUtil;

/**
 * 
 * BaseAction
 * @author he.jinguo
 * @version $Id: BaseAction.java, v 0.1 Jun 22, 2011 4:53:26 PM he.jinguo Exp $
 */
public class BaseAction {
    public String  sort;                       //排序字段
    public String  dir;                        //排序方式
    public Integer start = Integer.valueOf(0); //起始页
    public Integer limit = Integer.valueOf(25); //每页大小

    /**
     * 初始化分页
     * @return PagingBean    返回类型 
     */
    protected PagingBean getInitPagingBean() {
        String pageStart = getRequest().getParameter("start");//获得当前页数
        String pageLimit = getRequest().getParameter("limit");//获得每页显示的大小
        if (StringUtil.isNotEmpty(pageStart) && StringUtil.isNotEmpty(pageLimit)) {
            start = Integer.valueOf(pageStart);
            limit = Integer.valueOf(pageLimit);
            // if (start >= limit) {
            //     limit = start + limit;
            // }
        }
        return new PagingBean(this.start, this.limit);
    }

    /**
     * 初始化分页
     * @return PagingBean    返回类型 
     */
    protected PagingBean getInitPagingSortBean() {
        String pageStart = getRequest().getParameter("start");//获得当前页数
        String pageLimit = getRequest().getParameter("limit");//获得每页显示的大小
        sort = getRequest().getParameter("sort");
        dir = getRequest().getParameter("dir");
        if (StringUtil.isNotEmpty(pageStart) && StringUtil.isNotEmpty(pageLimit)) {
            start = Integer.valueOf(pageStart);
            limit = Integer.valueOf(pageLimit);
            //            if (start >= limit) {
            //                limit = start + limit;
            //            }
        }
        return new PagingBean(this.start, this.limit, this.sort, this.dir);
    }

    public Map<String, Object> getPageParams() {
        Map<String, Object> map = new HashMap<String, Object>();
        String pageStart = getRequest().getParameter("start");
        String pageLimit = getRequest().getParameter("limit");
        String sortType = getRequest().getParameter("sorttype");
        if ((pageStart != null && !pageStart.equals("")) && pageLimit != null) {
            Integer startnum = Integer.valueOf(pageStart);
            Integer limitnum = Integer.valueOf(pageLimit);
            if (startnum >= limitnum) {
                limitnum = startnum + limitnum;
            }
            map.put("start", startnum);
            map.put("limit", limitnum);
            map.put("sorttype", sortType);
        }
        return map;
    }

    /**
     * 获取用户登录信息
     */
    public AdminDO getUserSeesion() {
        return (AdminDO) getSession().getAttribute(PMSConstants.LOGIN_USER);
    }

    protected HttpServletRequest getRequest() {
        ServletRequestAttributes attrs = (ServletRequestAttributes) RequestContextHolder
            .getRequestAttributes();
        return attrs.getRequest();
    }

    protected HttpServletResponse getResponse() {
        ServletWebRequest attrs = (ServletWebRequest) RequestContextHolder.getRequestAttributes();
        return attrs.getResponse();
    }

    protected String getRealPath() {
        return this.getSession().getServletContext().getRealPath("/");
    }

    protected String getExcelFoderPath() {
        return getRealPath() + File.separator + "excel";
    }

    protected String getTempFoderPath() {
        return getRealPath() + File.separator + "temp";
    }

    protected String getFreeMarketFoderPath() {
        return getRealPath() + File.separator + "html";
    }

    protected HttpSession getSession() {
        return getRequest().getSession();
    }

}
